import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';

import * as JsBarcode from 'jsbarcode';
// @ts-ignore
import QRCode from 'qrcode';
import {UsedStorePService} from '../../../../used-store-p.service';

interface DATA {
  transType: number; // 1陆运 2航空
  promiseTimeType: any; // 产品名称
  deliveryId: string; // 运单号
  addedService: string; // 京尊达 当增值服务类型=京尊达时显示“尊”
  weight: string; // 重量
  sourceSortCenterName: string; // 始发分拣中心
  originalCrossCode: string; // 始发分拣滑道
  originalTabletorolleyCode: string; // 始发分拣笼车号
  targetSortCenterName: string; // 目的分拣中心
  destinationCrossCode: string; // 目的分拣滑道
  destinationTabletorolleyCode: string; // 目的分拣笼车号
  road: string; // 路区号
  siteName: string; // 派送营业部
  collectionValue: number; // 是否代收货款
  singReturn: any; // 签单返还
  receiverProvinceId: string; // 收件人地址
  receiverCityId: string; // 收件人地址
  receiverCountyId: string; // 收件人地址
  receiverAddress: string; // 收件人地址
  goods: string; // 托寄物
  orderId: string; // 订单号
  remark: string; // 备注
  goodsType: any; // 商品类型
}

enum SINGRETURN {
  '不返单',
  '普通返单',
  '校验身份返单',
  '电子签返单',
  '电子返单+普通返单',
}

enum GOODSTYPE {
  '普通' = 1,
  '生鲜常温',
  '鲜活' = 5,
  '控温',
  '冷藏',
  '冷冻',
  '深冷',
}

@Component({
  selector: 'app-print-logistics-jd',
  templateUrl: './print-logistics-jd.component.html',
  styleUrls: ['./print-logistics-jd.component.scss']
})
export class PrintLogisticsJdComponent implements OnInit, OnChanges {
  SINGRETURN = SINGRETURN;
  GOODSTYPE = GOODSTYPE;
  date = {
    day: '',
    time: '',
  };
  serialText = '';
  @Input() data: any = {};
  time = '';

  constructor() {
  }

  ngOnInit(): void {
  }


  ngOnChanges(changes: SimpleChanges) {
    if (this.data?.expressNum.match(/jd/i)) {
      this.data = {...this.data, ...JSON.parse(this.data.text)};
      this.data = {...this.data, ...this.data.preSortResult};
      Reflect.deleteProperty(this.data, 'text');
      Reflect.deleteProperty(this.data, 'preSortResult');
      console.log(this.data);
      this.time = new Date().toISOString().match(/\d{4}-\d{2}-\d{2}/) + ' '
        + new Date().toString().match(/\d{2}:\d{2}:\d{2}/);
      JsBarcode('.barcode', this.data.expressNum, {
        margin: 0,
        width: 1.8,
        height: 44,
        displayValue: false,
      });
      JsBarcode('.barcodeMin', this.data.expressNum, {
        margin: 0,
        width: 1,
        height: 30,
        displayValue: false,
      });
    }
    // const date = new Date();
    // this.date = {
    //   // @ts-ignore
    //   day: date.toISOString().match(/\d{4}-\d{2}-\d{2}/)[0],
    //   // @ts-ignore
    //   time: date.toString().match(/\d{2}:\d{2}:\d{2}/)[0],
    // };
    // if (this.data?.waybill_no) {
    //   const serialText = this.data?.waybill_no;
    //   JsBarcode('.barcode', serialText, {
    //     margin: 0,
    //     width: 2,
    //     height: 50,
    //     displayValue: false,
    //   });
    //   // tslint:disable-next-line:max-line-length
    //   this.serialText = '运单号： ' + serialText.slice(0, 2) + ' ' + serialText.slice(2, 5) + ' ' + serialText.slice(5, 8) + ' ' + serialText.slice(8, 11) + ' ' + serialText.slice(11, 15);
    // }
    // if (this.data?.two_dimension_code) {
    //   QRCode.toString(this.data?.two_dimension_code, {
    //     type: 'terminal',
    //     margin: 0
    //   }, (err: any, url: any) => {
    //     // @ts-ignore
    //     document.getElementById('qrcode').innerHTML = url;
    //   });
    // }
  }

  // promiseTimeType转换
  changePTT(type: any) {
    let text = '';
    switch (type) {
      case 1:
      case 7:
        text = '特惠送';
        break;
      case 4:
        text = '城际即日';
        break;
      case 8:
        text = '生鲜专送';
        break;
      case 14:
        text = '极速达';
        break;
      case 15:
        text = '同城速配';
        break;
      case 16:
        text = '生鲜特快';
        break;
      case 17:
        text = '生鲜特惠';
        break;
      case 20:
        text = '函速达';
        break;
      case 21:
        text = '特惠包裹';
        break;
    }
    return text;
  }
}
